/*
 * Copyright 2016 Wealth Inc. All rights reserved.
 */
package com.wealth.coupon.repository;

import iaas.repository.BaseRepository;

import java.util.Date;
import java.util.List;

import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import com.wealth.coupon.entity.CouponRecord;

/**
 * @author chan
 */
@Repository
public interface CouponRecordDao extends BaseRepository<CouponRecord, Long>,
		JpaSpecificationExecutor<CouponRecord> {

	/** 查询优惠券 会员+优惠券序列号 */
	List<CouponRecord> findByMemberIdAndCode(Long memberid, String code);

	/**
	 * 查询优惠券 会员可用优惠券 会员，最小订单金额，使用面值，状态，日期之内
	 */
	List<CouponRecord> findByMemberIdAndRuleMinAmountLessThanEqualAndRuleUsageAmountLessThanEqualAndStatusAndRuleStartTimeLessThanEqualAndRuleEndTimeGreaterThanEqual(
			Long memberid, Double total1, Double total2, Integer status,
			Date date1, Date date2);

	/**
	 * 查询某个优惠券 是否可用  优惠券Id，最小订单金额，使用面值，状态，日期之内
	 */
	CouponRecord findByIdAndAndRuleMinAmountLessThanEqualAndRuleUsageAmountLessThanEqualAndStatusAndRuleStartTimeLessThanEqualAndRuleEndTimeGreaterThanEqual(
			Long id, Double total1, Double total2, Integer status,
			Date date1, Date date2);

	/** 查询优惠券 会员+优惠券状态 */
	List<CouponRecord> findByMemberIdAndStatus(Long memberid, Integer status);

	/** 查询优惠券 会员 */
	List<CouponRecord> findByMemberId(Long memberid);
	
	
	/** 检查非纸质的优惠券批次 发放的数量*/
	@Query(value="SELECT count(*) FROM t_coupon_record a ,t_coupon_rule b,t_coupon c WHERE a.rule_id = b.id AND c.rule_id = b.id AND c.mode=? and c.corp_id=?",nativeQuery=true)
	Integer calcCouponRecordByCouponMode(Integer couponMode,Long corpid);
	
}
